package com.gisterrier.server;

import java.util.ArrayList;
import java.util.Set;
import java.util.TreeMap;

import com.common.db.DBConnection;
import com.common.shared.ClSelectionItem;
import com.gisterrier.shared.ClSelection;

public class ClSCGenerator {

	public static TreeMap<Long, ArrayList<ClSelectionItem>> tmRegions;
	public static TreeMap<Long, ArrayList<ClSelectionItem>> tmsubRegions;
	public static TreeMap<Long, ArrayList<ClSelectionItem>> tmcities;
	public static ArrayList<ClSelectionItem> regions;

	public static TreeMap<Integer, TreeMap<Long, ArrayList<ClSelectionItem>>> depValues;
	public static TreeMap<Integer, TreeMap<Long, ClSelectionItem>> values;
	public static TreeMap<Integer, ArrayList<ClSelectionItem>> parentItems;

	private static void generateValues() throws Exception {
		ClSelection[] clSelections = ClSelection.SELECTIONS;
		values = new TreeMap<Integer, TreeMap<Long, ClSelectionItem>>();
		TreeMap<Integer, ArrayList<ClSelectionItem>> fullValues = new TreeMap<Integer, ArrayList<ClSelectionItem>>();
		parentItems = new TreeMap<Integer, ArrayList<ClSelectionItem>>();

		for (int i = 0; i < clSelections.length; i++) {
			ClSelection ci = clSelections[i];
			ArrayList<ClSelectionItem> items = DBConnection.getSelectionItems(
					"Gass", ci.getSql(),
					ci.getParenttype() != ClSelection.T_NONE);
			fullValues.put(ci.getType(), items);
			values.put(ci.getType(), new TreeMap<Long, ClSelectionItem>());
			if (ci.getParenttype() == ClSelection.T_NONE)
				parentItems.put(ci.getType(), items);
		}
		Set<Integer> keys = fullValues.keySet();

		depValues = new TreeMap<Integer, TreeMap<Long, ArrayList<ClSelectionItem>>>();

		for (Integer key : keys) {
			ArrayList<ClSelectionItem> items = fullValues.get(key);
			TreeMap<Long, ClSelectionItem> valueItems = values.get(key);
			for (ClSelectionItem _item : items) {
				valueItems.put(_item.getId(), _item);
			}
			TreeMap<Long, ArrayList<ClSelectionItem>> map = new TreeMap<Long, ArrayList<ClSelectionItem>>();
			depValues.put(key, map);
			ClSelection k = getCild(key, clSelections);
			if (k == null)
				continue;
			ArrayList<ClSelectionItem> _items = fullValues.get(k.getType());
			if (_items == null)
				continue;
			for (ClSelectionItem _item : items) {
				map.put(_item.getId(), getSubItems(_item, _items));
			}

		}
	}

	private static ClSelection getCild(int parent, ClSelection[] clSelections) {
		for (ClSelection clSelection : clSelections) {
			if (clSelection.getParenttype() == parent)
				return clSelection;
		}
		return null;
	}

	public static ArrayList<ClSelectionItem> getRegions() {
		return getToptValues(ClSelection.T_REGION);
	}

	public static ArrayList<ClSelectionItem> getSubRegions(long regionId) {
		return getDepValues(ClSelection.T_REGION, regionId);
	}

	public static ArrayList<ClSelectionItem> getCities(long subRegionId) {
		return getDepValues(ClSelection.T_SUBREGION, subRegionId);

	}

	public static ArrayList<ClSelectionItem> getStreets(long cityId) {
		return getDepValues(ClSelection.T_CITY, cityId);
	}

	public static ClSelectionItem getValue(int type, long id) {
		TreeMap<Long, ClSelectionItem> items = values.get(type);
		if (items == null)
			return null;
		return items.get(id);
	}

	public static ArrayList<ClSelectionItem> getDepValues(int ptype,
			long parentId) {
		TreeMap<Long, ArrayList<ClSelectionItem>> items = depValues.get(ptype);
		if (items == null)
			return null;
		return items.get(parentId);
	}

	public static ArrayList<ClSelectionItem> getToptValues(int topType) {
		return parentItems.get(topType);
	}

	private static ArrayList<ClSelectionItem> getSubItems(
			ClSelectionItem clSelectionItem, ArrayList<ClSelectionItem> items) {
		ArrayList<ClSelectionItem> result = new ArrayList<ClSelectionItem>();
		for (ClSelectionItem item : items) {
			if (item.getParentId() == clSelectionItem.getId()) {
				result.add(item);
				item.setParentValue(clSelectionItem);
				clSelectionItem.getSubItems().add(item);
				result.add(item);
			}
		}
		return result;
	}

	static {
		try {
			generateValues();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
